//
//  dht-get.c
//  
//
//

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <unistd.h>
#include <time.h>
#include "dht.h"

//initialize the required varaible and spaces
char dht_node_ip[64];
char dir_id[64];
char send_data[1024],recv_data[1024];

struct sockaddr_in this_client_info;
struct sockaddr_in dht_node_info;
struct hostent *dht_node_host;

int this_client_socket;
int dht_node_port;
int bytes_recieved;

int main(int argc, char *argv[])
{
	if (argc !=3) {
		
		printf("Usage: \n %s <dhtnode> <id>\n", argv[0]);
		
	} else {
		
	  // Correctly format dhtnode.
	  char dht_node_input[64];
	  char *dht_node_in;
	  
	  //takes in the input from the user
	  strncpy(dht_node_input, argv[1], 64-1);
	  
	  //takes whatever is infront of the : and store it
	  dht_node_in = strtok(dht_node_input, ":");
	  
	  if(dht_node_in)
	    {
	      strncpy(dht_node_ip, dht_node_in, 64-1);
	      printf("DHT Node IP\t: %s\n", dht_node_ip);
	    }
	  //takes whatever is after the : and store it
	  dht_node_in = strtok(NULL, ":");
	  
	  if(dht_node_in) {
	    dht_node_port = atoi(dht_node_in);
	    printf("DHT Node Port\t: %i\n", dht_node_port);
	  }
		
        // Correctly format id.
        
	//takes in the ID from the user
        strncpy(dir_id, argv[2], 64-1);
        
        printf("The ID is\t: %s\n", dir_id);
        
        //stores info into *s, which will tell the node what to do
	size_t len1 = strlen(dir_id);
	char *s = malloc(len1+ 2);
        memcpy(s, "2|", 2);
        memcpy(s + 2, dir_id, len1);
        
	//connect to node
        int p = node_connect_get(dht_node_ip, dht_node_port, s);
        
        //printf("Connect\t: %i\n", p);
        
    }
}

//code to connect
int node_connect_get(int dst_ip[64], int dst_port, char send_data[512]){
  //creating socket using TCP
  if ((this_client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
    perror("Socket Error");
    exit(EXIT_FAILURE);
  }
    
  //dht_node_host = gethostbyname(dst_ip);
  
  this_client_info.sin_family = AF_INET; 
  inet_aton(dst_ip, &(this_client_info.sin_addr)); 
  this_client_info.sin_port = htons(dst_port);
  bzero(&(this_client_info.sin_zero), 8);
  printf("connected...\n");
  
  if (connect(this_client_socket, (struct sockaddr *)&this_client_info,
	      sizeof(struct sockaddr)) == -1)
    {
      perror("Connect Error\t");
      exit(1);
    }
  
  send(this_client_socket,send_data,strlen(send_data), 0);   
  //close(this_client_socket);
  
  while(1)
    {
      //receiving data send from the node
      bytes_recieved=recv(this_client_socket,recv_data,1024,0);
      recv_data[bytes_recieved] = '\0';
      
      if (bytes_recieved > 0) {
	//print out whatever is received
	printf("\nRecieved data = %s\n" , recv_data);
        close(this_client_socket);
	
        //printf("\nSEND (q or Q to quit) : ");
        fflush(stdout);
        break;
      }
    }  
  return 0;
  
  
}


